<?php
declare(strict_types=1);

namespace App\Service;

/**
 * Class GameService
 * @package App\Service
 */
class GameService
{
    const CARD_POOL = 'card_pool';

    //1 - 9    1万 - 9万         每个4张
    private static $wan = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    //11-19    1筒 - 9筒         每个4张
    private static $tong = [11, 12, 13, 14, 15, 16, 17, 18, 19];
    //21-29    1条 - 9条         每个4张
    private static $tiao = [21, 22, 23, 24, 25, 26, 27, 28, 29];
    //30-42    花牌 东南西北、春夏秋冬、梅兰竹菊 各一张
    private static $hua = [30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42];

    /**
     * 洗牌
     * @return array
     */
    public static function shuffleCards(): array
    {
        $cards = array_merge(
            self::copyArray(self::$wan, 4),
            self::copyArray(self::$tong, 4),
            self::copyArray(self::$tiao, 4),
            self::$hua
        );
        shuffle($cards);
        return $cards;
    }

    /**
     * 复制数组
     * @param array $arr 原数组
     * @param int $times 次数
     * @return array
     */
    public static function copyArray(array $arr, int $times): array
    {
        $data = [];
        for($i = 1; $i <= $times; $i++)
        {
            $data = array_merge($data, $arr);
        }
        return $data;
    }

    /**
     * 初始化卡池
     * @param array $cards
     * @param $room_id
     * @return false|int
     */
    public static function setCardPool(array $cards, $room_id)
    {
        redis()->del(self::CARD_POOL.$room_id);
        return redis()->rPush(self::CARD_POOL.$room_id, $cards);
    }
}